

<template>
  <div class="container">
    <div class="bread-container">
      <el-breadcrumb separator=">">
        <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
        <el-breadcrumb-item :to="{path: `/category/${categoryTwoData.parentId}`}">{{categoryTwoData.parentName}}</el-breadcrumb-item>
        <el-breadcrumb-item>{{categoryTwoData.name}}</el-breadcrumb-item>
      </el-breadcrumb>
    </div>
    <div class="sub-container">
      <el-tabs v-model="reqData.sortField" @tab-change="changeTab">
        <el-tab-pane label="最新商品" name="publishTime" />
        <el-tab-pane label="最高人气" name="orderNum" />
        <el-tab-pane label="评论最多" name="evaluateNum" />
      </el-tabs>
      <div class="body" v-infinite-scroll="load"  :infinite-scroll-disabled="scrollStatus">
        <HomeGoods v-for="good in goodList" :key="good.id" :goods="good" />
      </div>
    </div>
  </div>
</template>


<script setup  >
import { getCategoryFiteApI, getSubCategoryAPI } from '@/apis/category'
import { ref, onMounted } from 'vue'

import { useRoute } from 'vue-router'
import HomeGoods from '../Home/components/HomeGoods.vue'
let route = useRoute()
let scrollStatus = ref(false)
let categoryTwoData = ref([])
const getGoodsList = async () => {
  const res = await getCategoryFiteApI(route.params.id)
  console.log(res)
  categoryTwoData.value = res.result
}

// 二级分类商品相关数据
const goodList = ref([])

const reqData = ref({
  categoryId: route.params.id,
  sortField: 'publishTime',
  page: 1,
  pageSize: 20
})
/**
 * 请求二级标签的商品数据
 * 进入页面初始化数据.
 *
 **/
const getGoodsDataList = async () => {
  const res = await getSubCategoryAPI(reqData.value)
  goodList.value = res.result.items
}

onMounted(() => {
  getGoodsList()
  getGoodsDataList(reqData.value)
})

// tab栏切换展示数据

const flag = ref(false)

const changeTab = tab => {
  console.log(tab)
  reqData.value.sortField = tab
  reqData.value.page = 1
  getGoodsDataList()
}

// 加载更多数据
const load = async () => {
  reqData.value.page++
  const res = await getSubCategoryAPI(reqData.value)
  console.log(res)
  if(res.result.items.length === 0){
    scrollStatus.value = true
  }
  goodList.value = [...goodList.value, ...res.result.items]
  
}
</script>

<style lang="scss" scoped>
.bread-container {
  padding: 25px 0;
  color: #666;
}

.sub-container {
  padding: 20px 10px;
  background-color: #fff;

  .body {
    display: flex;
    flex-wrap: wrap;
    padding: 0 10px;
  }

  .goods-item {
    display: block;
    width: 220px;
    margin-right: 20px;
    padding: 20px 30px;
    text-align: center;

    img {
      width: 160px;
      height: 160px;
    }

    p {
      padding-top: 10px;
    }

    .name {
      font-size: 16px;
    }

    .desc {
      color: #999;
      height: 29px;
    }

    .price {
      color: $priceColor;
      font-size: 20px;
    }
  }

  .pagination-container {
    margin-top: 20px;
    display: flex;
    justify-content: center;
  }
}
</style>

function reqGetSubCategoryAPI(data: any) {
  throw new Error('Function not implemented.')
}
